/*
 * @Date: 2023-08-17 11:18:41
 * @LastEditors: 景冰川 961166897@qq.com
 * @LastEditTime: 2023-08-17 11:40:00
 * @FilePath: \veteran-cadre-app\src\composables\st\util.js
 */
/*
util.js

Copyright 1999-present Alibaba Group Holding Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('_')}_${[hour, minute, second].map(formatNumber).join('_')}`
}

function padding2(t) {
  if (t < 10)
    return `0${t}`

  return `${t}`
}

function utctimestamp() {
  const date = new Date()
  const YYYY = date.getUTCFullYear()
  const MM = padding2(date.getUTCMonth() + 1)
  const DD = padding2(date.getUTCDate())
  const HH = padding2(date.getUTCHours())
  const mm = padding2(date.getUTCMinutes())
  const ss = padding2(date.getUTCSeconds())
  return `${YYYY}-${MM}-${DD}T${HH}:${mm}:${ss}Z`
}

function uploadToSaver(f) {
  wx.uploadFile({
    url: 'http://30.224.161.18:9110/res/upload',
    filePath: f,
    name: 'file',
    success(res) {
      wx.removeSavedFile({
        filePath: f,
        success: () => {
          console.log(`remove ${f} done`)
        },
        fail: () => {
          console.log(`remove ${f} failed`)
        },
      })
    },
  })
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function sleep(milSec) {
  return new Promise((resolve) => {
    setTimeout(resolve, milSec)
  })
}

export const utils = {
  formatTime,
  uploadToSaver,
  utctimestamp,
  sleep,
}
// module.exports = {
//   formatTime,
//   uploadToSaver,
//   utctimestamp,
//   sleep,
// }
